package com.kaolafm.kradio.k_navi_map.location.amap;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.kaolafm.kradio.k_navi_map.location.OnLocationListener;
import com.kaolafm.kradio.k_navi_map.location.base.ILocation;
import com.kaolafm.kradio.k_navi_map.location.bean.MapLocationData;

/**
 * Created by kaolafm on 2018/4/15.
 */

public class AMapLocationManager implements ILocation {

    private AMapLocationClient mLocationclient;
    private AMapLocationClientOption mLocationOption;
    private AMapLocationListener mLocationListener;

    private OnLocationListener mOnLocationListener;

    public AMapLocationManager(Context context){
        init(context);
    }

    private void init(Context context){
        mLocationclient = new AMapLocationClient(context);
        mLocationOption = new AMapLocationClientOption();
        mLocationListener = new MyLocationListener();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setSensorEnable(true);
        mLocationclient.setLocationOption(mLocationOption);
        mLocationclient.setLocationListener(mLocationListener);
    }


    @Override
    public void startLocation(OnLocationListener onLocationListener) {
        if(mLocationclient != null){
            mOnLocationListener = onLocationListener;
            mLocationclient.startLocation();
        }
    }

    @Override
    public void stopLocation() {
        if(mLocationclient != null){
            mLocationclient.stopLocation();
        }
    }

    @Override
    public void onDestory() {
        if(mLocationclient != null){
            mLocationclient.onDestroy();
        }
    }

    @Override
    public void destroy() {

    }

    class MyLocationListener implements AMapLocationListener{

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation.getErrorCode() == 0 && mOnLocationListener != null){
                mOnLocationListener.locationSuccess(MapLocationData.transfer(aMapLocation));
                return;
            }
            mOnLocationListener.locationFailed();
        }
    }
}
